package com.zhdl.modules.slagRemoval.service;

import com.project.common.util.DelayUtil;
import com.zhdl.hardware.io.smartLink.repository.impl.IOCmdHandleDaoImpl;
import com.zhdl.hardware.io.smartLink.service.IOCmdSendService;
import com.zhdl.hardware.io_socket.service.IOWebsocketService;
import com.zhdl.hardware.motor.huichuan.repository.impl.HcPortCmdHandle;
import com.zhdl.hardware.motor.huichuan.service.HCCmdSendService;
import com.zhdl.modules.tools.CommonCallback;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.zhdl.modules.slagRemoval.SlagRemovalProcess.isRunning;

@Slf4j
@Service
public class SlagRemovalService {
    int xDistance = 70000, zDistance = 134000, pDistance = 98000;
    int xDistance1 = 70000, zDistance1 = 134000, pDistance1 = 96000;
    @Resource
    private IOCmdHandleDaoImpl ioHandleDao;
    @Resource
    private HcPortCmdHandle hcPortCmdHandle;
    @Resource
    private IOWebsocketService ioWebsocketService;
    @Resource
    private HCCmdSendService hcCmdSendService;

    @Resource
    private IOCmdSendService ioCmdSendService;

    public void init() {
        ioHandleDao.init("192.168.1.108:502");
        hcPortCmdHandle.addPort("7");
        hcPortCmdHandle.addPort("8");
        log.info("ioHandleDao" + ",hcCmdHandleDao init");
    }

    /**
     * 手动提起笼子
     * COM7发送指令>>>>0310110C0002049A2800015702<<<<<<<收到指令0310110c00028515
     */
    public void manualLiftCageOne(int xid) {

        if (xid == 1) {
            xDistance = 70000;
            zDistance = 100000;
        } else if (xid == 2) {
            xDistance = 890000;
            zDistance = 88000;
        }
        hcCmdSendService.motorMoveControl("7", "02", 165000, 2000, 0, 2000, s -> {
            log.info("电机上升到合适高度");
            hcCmdSendService.motorMoveControl("7", "01", xDistance, 1200, 0, 2000, s12 -> {
                log.info("电机移动到指定位置");
                hcCmdSendService.motorMoveControl("7", "02", zDistance, 2000, 0, 2000, s122 -> {
                    log.info("电机下降至提笼高度");
                    ioCmdSendService.ioWriteCoil05(2, true, s1 -> {
                        DelayUtil.delayedExecution(1000, () ->
                                hcCmdSendService.motorMoveDistance("7", "03", pDistance, 1000, 0, 2000, s10 -> {
                                    DelayUtil.delayedExecution(1000, () -> {
        //                                            hcCmdSendService.motorStatusStop("7","03");
                                        ioCmdSendService.ioWriteCoil05(2, false, s11 -> {
                                            log.info("电机抓取完成,状态码为:" + s11);
                                            hcCmdSendService.motorMoveControl("7", "02", 165000, 2000, 0, 2000, s20 -> {
                                                isRunning = false;
                                                log.info("电机提起高度");
                                            });
                                        });
                                    });
                        }));
                    });
                });
            });
        });

    }

    /**
     * 手动提起笼子
     * COM7发送指令>>>>0310110C0002049A2800015702<<<<<<<收到指令0310110c00028515
     */
    public void manualLiftCageOne1(int xid) {

        if (xid == 1) {
            xDistance1 = 0;
            zDistance1 = 750000;
        } else if (xid == 2) {
            xDistance1 = 1150000;
            zDistance1 = 400000;
        }
        hcCmdSendService.motorMoveControl("8", "02", 2700000, 2000, 0, 2000, s -> {
            log.info("电机上升到合适高度");
            hcCmdSendService.motorMoveControl("8", "01", xDistance1, 1200, 0, 2000, s12 -> {
                log.info("电机移动到指定位置");
                hcCmdSendService.motorMoveControl("8", "02", zDistance1, 2000, 0, 2000, s122 -> {
                    log.info("电机下降至提笼高度");
                    ioCmdSendService.ioWriteCoil05(2, true, s1 -> {
                        DelayUtil.delayedExecution(1000, () ->
                                hcCmdSendService.motorMoveDistance("8", "03", pDistance1, 1000, 0, 2000, s10 -> {
                                    DelayUtil.delayedExecution(1000, () -> {
                                        //                                            hcCmdSendService.motorStatusStop("7","03");
                                        ioCmdSendService.ioWriteCoil05(2, false, s11 -> {
                                            log.info("电机抓取完成,状态码为:" + s11);
                                            hcCmdSendService.motorMoveControl("8", "02", 2700000, 2000, 0, 2000, s20 -> {
                                                isRunning = false;
                                                log.info("电机提起高度");
                                            });
                                        });
                                    });
                                }));
                    });
                });
            });
        });

    }

    /**
     * 电机停止使能
     */
    public void manualStopReadyNS(int model){
        if (model == 0){
            hcCmdSendService.motorStatusStop("7","01");
            hcCmdSendService.motorStatusStop("7","02");
            hcCmdSendService.motorStatusStop("7","03");
        } else if (model == 1){
            hcCmdSendService.motorStatusStop("8","01");
            hcCmdSendService.motorStatusStop("8","02");
            hcCmdSendService.motorStatusStop("8","03");
        }

    }
    public void manualStartReadyNS(int model){
        if (model == 0){
            hcCmdSendService.motorStatusReady("7","01");
            hcCmdSendService.motorStatusReady("7","02");
            hcCmdSendService.motorStatusReady("7","03");
        } else if (model == 1){
            hcCmdSendService.motorStatusReady("8","01");
            hcCmdSendService.motorStatusReady("8","02");
            hcCmdSendService.motorStatusReady("8","03");
        }
    }

    public void manualBackOriginPickNS(int model){
        if (model == 0){
            hcCmdSendService.motorMoveControl("7", "03", 0, 1000, 0, 2000, s ->
                    log.info("北边电机夹爪松开完成"));

        } else if (model == 1){
            hcCmdSendService.motorMoveControl("8", "03", 0, 1000, 0, 2000, s ->
                    log.info("南边电机夹爪松开完成"));
        }
    }

    /**
     * 电机回原点功能
     */
    public void manualBackOriginNS(int model) {
        if (model == 0) {
            hcCmdSendService.motorMoveControl("7", "03", 0, 1000, 0, 2000, s ->
                    DelayUtil.delayedExecution(3000, () ->
                            hcCmdSendService.motorMoveControl("7", "02", 165000, 2000, 0, 2000, s1 ->
                                    DelayUtil.delayedExecution(1000, () ->
                                            hcCmdSendService.motorMoveControl("7", "01", 70000, 1200, 0, 2000, s11 ->
                                            {
                                                DelayUtil.delayedExecution(2000, () ->
                                                        hcCmdSendService.motorMoveControl("7", "02", 220000, 2000, 0, 2000, s2 -> {
                                                        isRunning = false;
                                                        log.info("电机回原点完成");
                                                }));
                                            })))));
        } else if (model == 1){
            hcCmdSendService.motorMoveControl("8", "03", 0, 1000, 0, 2000, s ->
                    DelayUtil.delayedExecution(3000, () ->
                            hcCmdSendService.motorMoveControl("8", "02", 2700000, 2000, 0, 2000, s1 ->
                                    DelayUtil.delayedExecution(1000, () ->
                                            hcCmdSendService.motorMoveControl("8", "01", 0, 1200, 0, 2000, s11 ->
                                            {
                                                DelayUtil.delayedExecution(2000, () ->
                                                        hcCmdSendService.motorMoveControl("8", "02", 4300000, 2000, 0, 2000, s2 -> {
                                                        isRunning = false;
                                                        log.info("电机回原点完成");
                                                }));
                                            })))));
        }

    }

    /**
     * 手动放下笼子
     */
    public void manualPutDownCage(int xid) {
        if (xid == 1) {
            xDistance = 70000;
            zDistance = 100000;
        } else if (xid == 2) {
            xDistance = 890000;
            zDistance = 88000;
        }
        hcCmdSendService.motorMoveControl("7", "02", 165000, 2000, 0, 2000, s -> {
            log.info("电机上升到合适高度");
            hcCmdSendService.motorMoveControl("7", "01", xDistance, 1200, 0, 2000, s12 -> {
                log.info("电机移动到指定位置");
                hcCmdSendService.motorMoveControl("7", "02", zDistance, 2000, 0, 2000, s122 -> {
                    log.info("电机下降至提笼高度");
                    ioCmdSendService.ioWriteCoil05(2, true, s1 -> {
                        hcCmdSendService.motorMoveDistance("7", "03", 0, 1000, 0, 2000, s10 -> {
//                            hcCmdSendService.motorStatusStop("7","03");
                            ioCmdSendService.ioWriteCoil05(2, false, s11 -> {
                                log.info("电机松开完成,状态码为:" + s11);
                                hcCmdSendService.motorMoveControl("7", "02", 165000, 2000, 0, 2000, s20 -> {
                                    log.info("电机提起高度");
                                    hcCmdSendService.motorMoveControl("7", "01", 70000, 1200, 0, 2000, s2 -> {
                                        hcCmdSendService.motorMoveControl("7", "02", 220000, 2000, 0, 2000, s3 -> {
                                            isRunning = false;
                                            log.info("x轴回到原点");
                                        });
                                    });
                                });
                            });
                        });
                    });
                });
            });
        });
    }

    /**
     * 手动放下笼子
     */
    public void manualPutDownCage1(int xid) {
        if (xid == 1) {
            xDistance1 = 0;
            zDistance1 = 750000;
        } else if (xid == 2) {
            xDistance1 = 1150000;
            zDistance1 = 400000;
        }
        hcCmdSendService.motorMoveControl("8", "02", 2700000, 2000, 0, 2000, s -> {
            log.info("电机上升到合适高度");
            hcCmdSendService.motorMoveControl("8", "01", xDistance1, 1200, 0, 2000, s12 -> {
                log.info("电机移动到指定位置");
                hcCmdSendService.motorMoveControl("8", "02", zDistance1, 2000, 0, 2000, s122 -> {
                    log.info("电机下降至提笼高度");
                    ioCmdSendService.ioWriteCoil05(2, true, s1 -> {
                        hcCmdSendService.motorMoveDistance("8", "03", 0, 1000, 0, 2000, s10 -> {
//                            hcCmdSendService.motorStatusStop("7","03");
                            ioCmdSendService.ioWriteCoil05(2, false, s11 -> {
                                log.info("电机松开完成,状态码为:" + s11);
                                hcCmdSendService.motorMoveControl("8", "02", 2700000, 2000, 0, 2000, s20 -> {
                                    log.info("电机提起高度");
                                    hcCmdSendService.motorMoveControl("8", "01", 0, 1200, 0, 2000, s2 -> {
                                        hcCmdSendService.motorMoveControl("8", "02", 4300000, 2000, 0, 2000, s3 -> {
                                            isRunning = false;
                                            log.info("x轴回到原点");
                                        });
                                    });
                                });
                            });
                        });
                    });
                });
            });
        });
    }

}
